package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.common.utils.StringUtils;

import com.itheima.order.entity.OmsOrderReturnApply;
import com.itheima.service.OmsOrderReturnApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

//关于后台退货订单
@Slf4j
@RestController
@RequestMapping("/returnApply")
public class OmsOrderReturnApplyController {
    @Autowired
    private OmsOrderReturnApplyService omsOrderReturnApplyService;

    /**
     * 批量删除退单申请
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    private AjaxResult delete(String ids) {
        List<Long> longs = StringUtils.toList(ids);
        omsOrderReturnApplyService.removeByIds(longs);
        return AjaxResult.success();
    }

    /**
     * 分页查询退单申请
     *
     * @param creatTime
     * @param handleMan
     * @param handleTime
     * @param id
     * @param pageNum
     * @param pageSize
     * @param receiverKeyword
     * @param status
     * @return
     */
    @GetMapping("/list")
    private AjaxResult list(@RequestParam(value = "createTime", required = false) String creatTime, @RequestParam(value = "handleMan", required = false) String handleMan, @RequestParam(value = "handleTime", required = false) String handleTime, @RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize, @RequestParam(value = "receiverKeyword", required = false) String receiverKeyword, @RequestParam(value = "status", required = false) Integer status) {
        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);
        lqw.like(creatTime != null, OmsOrderReturnApply::getCreateTime, creatTime)
                .like(handleMan != null, OmsOrderReturnApply::getHandleMan, handleMan)
                .like(handleTime != null, OmsOrderReturnApply::getHandleTime, handleTime)
                .like(id != null, OmsOrderReturnApply::getId, id)
                .like(receiverKeyword != null, OmsOrderReturnApply::getReturnName, receiverKeyword)
                .like(receiverKeyword != null, OmsOrderReturnApply::getReturnPhone, receiverKeyword)
                .eq(status != null, OmsOrderReturnApply::getStatus, status);

        omsOrderReturnApplyService.page(page, lqw);
        PageInfo pageInfo = PageInfo.toPageList(page);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 修改退单状态
     *
     * @param id
     * @param omsOrderReturnApply
     * @return
     */
    @PostMapping("/update/status/{id}")
    private AjaxResult orderStatus(@PathVariable String id, @RequestBody OmsOrderReturnApply omsOrderReturnApply) {
        LambdaUpdateWrapper<OmsOrderReturnApply> luw = new LambdaUpdateWrapper<>();
        if (omsOrderReturnApply.getStatus() == 2) {
            luw.eq(OmsOrderReturnApply::getId, id)
                    .set(omsOrderReturnApply.getCompanyAddressId() != null, OmsOrderReturnApply::getCompanyAddressId, omsOrderReturnApply.getCompanyAddressId())
                    .set(omsOrderReturnApply.getHandleMan() != null, OmsOrderReturnApply::getHandleMan, omsOrderReturnApply.getHandleMan())
                    .set(omsOrderReturnApply.getHandleNote() != null, OmsOrderReturnApply::getHandleNote, omsOrderReturnApply.getHandleNote())
                    .set(omsOrderReturnApply.getReceiveMan() != null, OmsOrderReturnApply::getReceiveMan, omsOrderReturnApply.getReceiveMan())
                    .set(omsOrderReturnApply.getReceiveNote() != null, OmsOrderReturnApply::getReceiveNote, omsOrderReturnApply.getReceiveNote())
                    .set(omsOrderReturnApply.getReturnAmount() != null, OmsOrderReturnApply::getReturnAmount, omsOrderReturnApply.getReturnAmount())
                    .set(omsOrderReturnApply.getStatus() != null, OmsOrderReturnApply::getStatus, omsOrderReturnApply.getStatus())
                    .set(OmsOrderReturnApply::getReceiveTime, LocalDateTime.now());
            omsOrderReturnApplyService.update(luw);
            return AjaxResult.success();
        } else {
            luw.eq(OmsOrderReturnApply::getId, id)
                    .set(omsOrderReturnApply.getCompanyAddressId() != null, OmsOrderReturnApply::getCompanyAddressId, omsOrderReturnApply.getCompanyAddressId())
                    .set(omsOrderReturnApply.getHandleMan() != null, OmsOrderReturnApply::getHandleMan, omsOrderReturnApply.getHandleMan())
                    .set(omsOrderReturnApply.getHandleNote() != null, OmsOrderReturnApply::getHandleNote, omsOrderReturnApply.getHandleNote())
                    .set(omsOrderReturnApply.getReceiveMan() != null, OmsOrderReturnApply::getReceiveMan, omsOrderReturnApply.getReceiveMan())
                    .set(omsOrderReturnApply.getReceiveNote() != null, OmsOrderReturnApply::getReceiveNote, omsOrderReturnApply.getReceiveNote())
                    .set(omsOrderReturnApply.getReturnAmount() != null, OmsOrderReturnApply::getReturnAmount, omsOrderReturnApply.getReturnAmount())
                    .set(omsOrderReturnApply.getStatus() != null, OmsOrderReturnApply::getStatus, omsOrderReturnApply.getStatus())
                    .set(OmsOrderReturnApply::getHandleTime, LocalDateTime.now());
            omsOrderReturnApplyService.update(luw);
            return AjaxResult.success();
        }
    }

    /**
     * 根据id获取退单
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private AjaxResult getById(@PathVariable Long id) {
        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyService.getById(id);
        return AjaxResult.success(omsOrderReturnApply);
    }
}
